package org.cocos2dx.javascript.admob;

import android.app.Activity;
import android.os.Looper;
import android.util.Log;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.OnUserEarnedRewardListener;
import com.google.android.gms.ads.rewarded.RewardItem;
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd;
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback;
import com.logicgame.carmerge.R;

import org.cocos2dx.javascript.AppActivity;
import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;

import androidx.annotation.NonNull;

public class Interstitial_Reward {
    private static RewardedInterstitialAd rewardedInterstitialAd;
    static Activity app;

    static int reword = 0;
    private static final String TAG = "Admob";
    public static void loadAd(final Activity _app) {
        app = _app;
        final String INTERSTITIAL_REWARD_AD_UNIT_ID = app.getString(R.string.INTERSTITIAL_REWARD_AD_UNIT_ID);
        RewardedInterstitialAd.load(app, INTERSTITIAL_REWARD_AD_UNIT_ID,
                new AdRequest.Builder().build(),  new RewardedInterstitialAdLoadCallback() {
                    @Override
                    public void onAdLoaded(RewardedInterstitialAd ad) {
                        rewardedInterstitialAd = ad;
                        Log.d(TAG, "onAdLoaded");
                    }
                    @Override
                    public void onAdFailedToLoad(LoadAdError loadAdError) {
                        Log.e(TAG, "onAdFailedToLoad");
                        Ad_Fail_Timer.reload(4,app);
                    }
                });
    };
    public static int GetInterstitialRewardAD(){
        if(rewardedInterstitialAd == null){
            return 0;
        }else{
            return 1;
        }
    }
    /**
     * @return
     * 0:代表无广告显示
     * 1：代表广告正常显示完毕，需要发放奖励
     */
    public static int ShowInterstitialRewardAD(){
        //final AppActivity cocosapp = new AppActivity();
        if (rewardedInterstitialAd == null) {
            Log.d("TAG", "The rewarded ad wasn't ready yet.");
            return 0;
        }
        app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.v(TAG,"show inter_rewardAD in admob.inter_reward");
                rewardedInterstitialAd.setFullScreenContentCallback(
                        new FullScreenContentCallback() {
                            @Override
                            public void onAdShowedFullScreenContent() {
                                // Called when ad is shown.
                                Log.d(TAG, "onAdShowedFullScreenContent");
                                //Toast.makeText(MainActivity.this, "onAdShowedFullScreenContent", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onAdFailedToShowFullScreenContent(AdError adError) {
                                // Called when ad fails to show.
                                Log.d(TAG, "onAdFailedToShowFullScreenContent");
                                // Don't forget to set the ad reference to null so you
                                // don't show the ad a second time.
                                rewardedInterstitialAd = null;
                                reword = 0;
                                Ad_Fail_Timer.reload(4,app);
                                //Toast.makeText(MainActivity.this, "onAdFailedToShowFullScreenContent", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onAdDismissedFullScreenContent() {
                                // Called when ad is dismissed.
                                // Don't forget to set the ad reference to null so you
                                // don't show the ad a second time.
                                rewardedInterstitialAd = null;
                                Log.d(TAG, "onAdDismissedFullScreenContent");
                                org.cocos2dx.lib.Cocos2dxHelper.runOnGLThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if(reword == 1){
                                            org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge.evalString("Admob.ShowRewordDialog()");
                                        }
                                    }
                                });
                                Ad_Fail_Timer.reload(4,app);
                            }
                        });
                Activity activityContext = app;
                rewardedInterstitialAd.show(
                        activityContext,
                        new OnUserEarnedRewardListener() {
                            @Override
                            public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                                Log.d(TAG, "The user earned the reward.");
                                reword = 1;
                                org.cocos2dx.lib.Cocos2dxHelper.runOnGLThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge.evalString("Admob.Reword(\"coin\",Global.CurrCarsEarn * 3)");
                                    }
                                });
                            }
                        });
            }
        });
        return reword;
    }

}
